package net.lesscoding.server.handler;

import cn.hutool.core.util.StrUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.lesscoding.message.GroupMembersRequestMessage;
import net.lesscoding.message.GroupMembersResponseMessage;
import net.lesscoding.session.Group;
import net.lesscoding.session.GroupSession;
import net.lesscoding.session.SessionFactory;

import java.util.Set;

/**
 * @author eleven
 * @date 2024/10/31 16:21
 * @apiNote
 */
@ChannelHandler.Sharable
public class GroupMemberRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage msg) throws Exception {
        String groupName = msg.getGroupName();
        GroupSession groupSession = SessionFactory.groupSession();
        Set<String> members = groupSession.getGroupMembers(groupName);
        Group group = groupSession.getGroup(groupName);
        if (group != null) {
            ctx.writeAndFlush(new GroupMembersResponseMessage(true, StrUtil.format("全部成员 {}", members)));
        } else {
            ctx.writeAndFlush(new GroupMembersResponseMessage(false, StrUtil.format("群聊{}不存在", groupName)));
        }
    }
}
